package com.thlws.study.rabbitmq.controller;

import com.thlws.study.rabbitmq.internal.RabbitMqSender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

/**
 * @author HanleyTang 2019-08-01
 */
@RestController
public class IndexController {

    @Autowired
    RabbitMqSender rabbitMqSender;

    /**
     * http://localhost:9801/send?queue=queue1
     * http://localhost:9801/send?queue=queue2
     */
    @RequestMapping("/send")
    public Object send(@RequestParam(name = "queue",required = false) String queue){
        rabbitMqSender.send(queue, queue+" 的数据:"+System.currentTimeMillis()+"");
        return LocalDateTime.now();
    }

    /**
     * http://localhost:9801/fanout/send?exchange=myFanoutExChange
     */
    @RequestMapping("/fanout/send")
    public Object fanoutSend(@RequestParam(name = "exchange") String exchange){
        rabbitMqSender.send(exchange ,null,"fanout :"+System.currentTimeMillis()+"");
        return LocalDateTime.now();
    }

    /**
     * http://localhost:9801/direct/send?exchange=myDirectExChange&routingKey=user.info
     * http://localhost:9801/direct/send?exchange=myDirectExChange&routingKey=user.work
     */
    @RequestMapping("/direct/send")
    public Object directSend(@RequestParam(name = "exchange") String exchange,
                             @RequestParam(name = "routingKey") String routingKey){

        rabbitMqSender.send(exchange ,routingKey,"direct :"+System.currentTimeMillis()+"");
        return LocalDateTime.now();
    }


    /**
     * http://localhost:9801/topic/send?exchange=myTopicExChange&routingKey=user.info
     * http://localhost:9801/topic/send?exchange=myTopicExChange&routingKey=user.xxx
     * http://localhost:9801/topic/send?exchange=myTopicExChange&routingKey=user.work
     */
    @RequestMapping("/topic/send")
    public Object topicSend(@RequestParam(name = "exchange") String exchange,
                             @RequestParam(name = "routingKey") String routingKey){

        rabbitMqSender.send(exchange ,routingKey,"topic :"+System.currentTimeMillis()+"");
        return LocalDateTime.now();
    }
}
